import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:decimal/decimal.dart';
import 'package:ls_work/net/response/food/week_choose_data.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/util/request_url_utils.dart';

/// orderModelId : 1
/// orderModelName : null
/// orderType : null
/// name : "零点菜单"
/// isWeek : 1
/// menuJson : {"branchId":1000,"name":"零点菜单","tenantId":1000,"orderModelId":1,"saasId":1000,"remark":"这是一个备注","weekMenuModel":1,"content":[{"ruleCategory":[{"categary":"主食","selectNumber":2,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]},{"weekDay":2,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]},{"categary":"小吃","selectNumber":1,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]}],"range":"05:00-09:00","timeSection":"breakfast"}],"siteList":[{"siteName":"桌1","id":4},{"siteName":"南北食堂大杂烩","id":6}]}
/// menuStartDate : "2022-03-21 00:00:00"
/// menuEndDate : "2022-03-27 00:00:00"
/// status : 0
/// isDel : null
/// remark : "这是一个备注"
/// branchId : 1000

class WeekMenuListData {
  WeekMenuListData(
      {this.orderModelId,
      this.orderModelName,
      this.orderType,
      this.name,
      this.isWeek,
      this.menuJson,
      this.menuStartDate,
      this.menuEndDate,
      this.status,
      this.isDel,
      this.remark,
      this.branchId,
      this.tenantId,
      this.saasId,
      this.menuId});

  WeekMenuListData.fromJson(dynamic json) {
    orderModelId = json['orderModelId'];
    orderModelName = json['orderModelName'];
    orderType = json['orderType'];
    name = json['name'];
    isWeek = json['isWeek'];
    menuJson = json['menuJson'] != null
        ? WeekMenuJson.fromJson(json['menuJson'])
        : null;
    menuStartDate = json['menuStartDate'];
    menuEndDate = json['menuEndDate'];
    status = json['status'];
    isDel = json['isDel'];
    remark = json['remark'];
    branchId = json['branchId'];
    tenantId = json['tenantId'];
    saasId = json['saasId'];
    menuId = json['menuId'];
  }

  int? orderModelId;
  dynamic orderModelName;
  dynamic orderType;
  String? name;
  int? isWeek;
  WeekMenuJson? menuJson;
  String? menuStartDate;
  String? menuEndDate;
  int? status;
  dynamic isDel;
  String? remark;
  dynamic branchId;
  dynamic saasId;
  dynamic tenantId;
  dynamic menuId;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['orderModelId'] = orderModelId;
    map['orderModelName'] = orderModelName;
    map['orderType'] = orderType;
    map['name'] = name;
    map['isWeek'] = isWeek;
    if (menuJson != null) {
      map['menuJson'] = menuJson?.toJson();
    }
    map['menuStartDate'] = menuStartDate;
    map['menuEndDate'] = menuEndDate;
    map['status'] = status;
    map['isDel'] = isDel;
    map['remark'] = remark;
    map['branchId'] = branchId;
    map['saasId'] = saasId;
    map['tenantId'] = tenantId;
    map['menuId'] = menuId;
    return map;
  }
}

/// branchId : 1000
/// name : "零点菜单"
/// tenantId : 1000
/// orderModelId : 1
/// saasId : 1000
/// remark : "这是一个备注"
/// weekMenuModel : 1
/// content : [{"ruleCategory":[{"categary":"主食","selectNumber":2,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]},{"weekDay":2,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]},{"categary":"小吃","selectNumber":1,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]}],"range":"05:00-09:00","timeSection":"breakfast"}]
/// siteList : [{"siteName":"桌1","id":4},{"siteName":"南北食堂大杂烩","id":6}]

class WeekMenuJson {
  WeekMenuJson({
    this.branchId,
    this.name,
    this.tenantId,
    this.orderModelId,
    this.saasId,
    this.remark,
    this.weekMenuModel,
    this.content,
    this.siteList,
  });

  WeekMenuJson.fromJson(dynamic json) {
    branchId = json['branchId'];
    name = json['name'];
    tenantId = json['tenantId'];
    orderModelId = json['orderModelId'];
    saasId = json['saasId'];
    remark = json['remark'];
    weekMenuModel = json['weekMenuModel'];
    if (json['content'] != null) {
      content = [];
      json['content'].forEach((v) {
        content?.add(WeekMenuContent.fromJson(v));
      });
    }
    if (json['siteList'] != null) {
      siteList = [];
      json['siteList'].forEach((v) {
        siteList?.add(SiteList.fromJson(v));
      });
    }
  }

  int? branchId;
  String? name;
  int? tenantId;
  int? orderModelId;
  int? saasId;
  String? remark;
  dynamic weekMenuModel;
  List<WeekMenuContent>? content;
  List<SiteList>? siteList;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['branchId'] = branchId;
    map['name'] = name;
    map['tenantId'] = tenantId;
    map['orderModelId'] = orderModelId;
    map['saasId'] = saasId;
    map['remark'] = remark;
    map['weekMenuModel'] = weekMenuModel;
    if (content != null) {
      map['content'] = content?.map((v) => v.toJson()).toList();
    }
    if (siteList != null) {
      map['siteList'] = siteList?.map((v) => v.toJson()).toList();
    }
    return map;
  }
}

/// siteName : "桌1"
/// id : 4

class SiteList {
  SiteList({
    this.siteName,
    this.id,
  });

  SiteList.fromJson(dynamic json) {
    siteName = json['siteName'];
    id = json['id'];
  }

  String? siteName;
  dynamic id;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['siteName'] = siteName;
    map['id'] = id;
    return map;
  }
}

/// ruleCategory : [{"categary":"主食","selectNumber":2,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]},{"weekDay":2,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]},{"categary":"小吃","selectNumber":1,"goodsInfo":[{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]}]
/// range : "05:00-09:00"
/// timeSection : "breakfast"

class WeekMenuContent {
  int? foodLength = 0;

  WeekMenuContent(
      {this.ruleCategory,
      this.range,
      this.timeSection,
      this.rangeStart,
      this.rangeEnd});

  WeekMenuContent.fromJson(dynamic json) {
    if (json['ruleCategory'] != null) {
      ruleCategory = [];
      json['ruleCategory'].forEach((v) {
        ruleCategory?.add(RuleCategory.fromJson(v));
      });
    }
    range = json['range'];
    rangeStart = json['rangeStart'];
    rangeEnd = json['rangeEnd'];
    timeSection = (json['timeSection'] is int)
        ? json['timeSection']
        : json['timeSection'].toString().toInt();
  }

  List<RuleCategory>? ruleCategory;
  List<GoodsInfo> goodsInfo = [];

  List<GoodsInfo> get showGoodsInfo {
    if (RequestUrlUtils.code == "xcyg") {
      var now = DateTime.now();
      var today = DateTime(now.year,now.month,now.day);
      return goodsInfo.takeWhere<GoodsInfo>(
          (element) => element.weekDate!.isAfter(today));
    } else {
      return goodsInfo;
    }
  }

  String? range;
  String? rangeStart;
  String? rangeEnd;
  int? timeSection;

  String get timeStr => timeSection == 1
      ? '早餐'
      : timeSection == 2
          ? "午餐"
          : '晚餐';

  String get startTime {
    if (!rangeStart.isNullOrEmpty()) {
      return rangeStart??"";
    }
    if (range.isNullOrEmpty() || !range!.contains('-')) {
      return "";
    } else {
      return range!.split('-')[0];
    }
  }

  String get endTime {
    if (!rangeEnd.isNullOrEmpty()) {
      return rangeEnd!;
    }
    if (range.isNullOrEmpty() || !range!.contains('-')) {
      return "";
    } else {
      return range!.split('-')[1];
    }
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    if (ruleCategory != null) {
      map['ruleCategory'] = ruleCategory!.map((v) => v.toJson()).toList();
    }
    map['range'] = range;
    map['timeSection'] = timeSection;
    map['rangeStart'] = rangeStart;
    map['rangeEnd'] = rangeEnd;
    return map;
  }

  Map<String, dynamic> toSubmitJson() {
    var map = <String, dynamic>{"timeSection": timeSection};
    if (ruleCategory != null) {
      map['ruleCategory'] = ruleCategory!.map((v) => v.toSubmitJson()).toList();
    }
    return map;
  }
}

/// categary : "主食"
/// selectNumber : 2
/// goodsInfo : [{"weekDay":1,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]},{"weekDay":2,"goodsList":[{"id":21,"goodsName":"荤菜hahhahha"}]}]

class RuleCategory {
  RuleCategory({
    this.categary,
    this.selectNumber,
    this.goodsInfo,
  });

  RuleCategory.fromJson(dynamic json) {
    categary = json['categary'];
    var number = json['selectNumber'];
    if (number is String) {
      selectNumber = number.isEmpty ? 0 : number.toInt();
    } else {
      selectNumber = json['selectNumber'];
    }

    if (json['goodsInfo'] != null) {
      goodsInfo = [];
      json['goodsInfo'].forEach((v) {
        goodsInfo?.add(GoodsInfo.fromJson(v));
      });
    }
  }

  String? categary;
  int? selectNumber;
  int? foodLength = 0;
  List<GoodsInfo>? goodsInfo;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['categary'] = categary;
    map['selectNumber'] = selectNumber;
    if (goodsInfo != null) {
      map['goodsInfo'] = goodsInfo?.map((v) => v.toJson()).toList();
    }
    return map;
  }

  Map<String, dynamic> toSubmitJson() {
    final map = <String, dynamic>{};
    map['categary'] = categary;
    map['selectNumber'] = selectNumber;
    if (goodsInfo != null) {
      map['goodsInfo'] = goodsInfo?.map((v) => v.toSubmitJson()).toList();
    }
    return map;
  }
}

/// weekDay : 1
/// goodsList : [{"id":21,"goodsName":"荤菜hahhahha"}]

class GoodsInfo {
  String? timeTypeId;
  int? timeSection;
  String? rangeStart;
  String? rangeEnd;
  var isCanChoose = false;

  GoodsInfo({
    this.weekDay,
    this.goodsList,
  });

  GoodsInfo.fromJson(dynamic json) {
    weekDay = json['weekDay'].toString().toInt();
    if (json['goodsList'] != null) {
      goodsList = [];
      json['goodsList'].forEach((v) {
        goodsList?.add(GoodsList.fromJson(v));
      });
    }
  }

  int? weekDay;
  DateTime? weekDate;
  List<GoodsList>? goodsList;
  List<GoodsList> selectItems = [];
  List<WeekChooseData> chooseData = [];
  num? limitPrice;
  bool hasLimit = false;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is GoodsInfo &&
          runtimeType == other.runtimeType &&
          timeSection == other.timeSection &&
          weekDay == other.weekDay;

  @override
  int get hashCode => timeSection.hashCode ^ weekDay.hashCode;

  double selectPrice() {
    var price = 0.0;
    for (var good in goodsList??<GoodsList>[]) {
      price += (good.price! * (good.number??0));
    }
    return price.toStringAsFixed(1).toDouble()!;
  }

  double paidPrice() {
    var price = 0.0;
    for (var item in goodsList ?? <GoodsList>[]) {
      if (1 == (item.oldStatus ?? 0)) {
        price += (item.oldNumber ?? 0) * item.price!;
      }
    }
    return price.toStringAsFixed(1).toDouble()!;
  }

  double finalPrice() {
    var paidPrices = paidPrice().d;
    var selectPrices = selectPrice().d;
    Decimal finalPrice = Decimal.parse("0");
    if (hasLimit) {
      if (paidPrices!.toDouble() == 0) {
        finalPrice = maxD(selectPrices - limitPrice!.d, 0.d);
      } else if (paidPrices >=( limitPrice.d ?? 0.d)) {
        if (selectPrices <= limitPrice!.d) {
          finalPrice = (limitPrice!.d - paidPrices);
        } else {
          finalPrice = selectPrices - paidPrices;
        }
      } else if (paidPrices < (limitPrice!.d ?? 0.d)) {
        finalPrice =
            maxD(selectPrices - paidPrices - (limitPrice!.d - paidPrices), 0.d);
      } else {
        finalPrice = maxD(selectPrices - (paidPrices - limitPrice!.d), 0.d);
      }
    } else {
      finalPrice = selectPrices - paidPrices;
    }

    return finalPrice.toDouble();
  }

  int? maxnumber;
  int? foodLength = 0;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['weekDay'] = weekDay;
    if (goodsList != null) {
      map['goodsList'] = goodsList!.map((v) => v.toJson()).toList();
    }
    return map;
  }

  Map<String, dynamic> toSubmitJson() {
    final map = <String, dynamic>{};
    map['weekDay'] = weekDay;
    if (goodsList != null) {
      map['goodsList'] = goodsList!.map((v) => v.toSubmitJson()).toList();
    }
    return map;
  }
}

/// id : 21
/// goodsName : "荤菜hahhahha"

class GoodsList {
  String? foodEndTime;

  String? foodDate;

  bool canSelect = false;

  bool selected = false;

  String? canSelectRemark;

  int? status = 0;

  int? oldStatus = 0;

  int? oldNumber = 0;

  num? price;

  int? isTake;

  int? timeSection;

  DateTime? weekDate;

  String? rangeEnd;

  String? attachIds;

  num? limitAmount;

  String? name;

  GoodsList(
      {this.id,
      this.goodsName,
      this.status,
      this.price,
      this.number = 0,
      this.isTake,
      this.oldStatus,
      this.canSelect = true,
      this.canSelectRemark,
      this.attachIds,
      this.name});

  String get foodImageId => (attachIds??"").split(",")[0];

  GoodsList.fromJson(dynamic json) {
    id = (json['id'] is int) ? json['id'] : json['id'].toString().toInt();
    goodsName = json['goodsName'];
    status = json['status'];
    // oldStatus = json['status'];
    oldStatus = status;

    isTake = json['isTake'];
    canSelect = json['canSelect'] ?? true;
    canSelectRemark = json['canSelectRemark'];
    attachIds = json['attachIds'];
    limitAmount = json['limitAmount'];
    name = json['name'];
    price =
        json['price'] == null ? 0 : json['price'].toString().toDouble() ?? 0;
    selected = status == 1;

    var foodNumber = json['num'];
    number = status == 0 ? 0 : foodNumber;
  }

  dynamic id;
  String? goodsName;
  int? number = 0;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['goodsName'] = goodsName;
    map['status'] = status;
    map['oldStatus'] = oldStatus;
    map['price'] = price;
    map['num'] = number;
    map['isTake'] = isTake;
    map['canSelect'] = canSelect;
    map['canSelectRemark'] = canSelectRemark;
    map['attachIds'] = attachIds;
    map['limitAmount'] = limitAmount;
    map['name'] = name;
    return map;
  }

  Map<String, dynamic> toSubmitJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['goodsName'] = goodsName;
    map['price'] = price;
    map['status'] = status ?? 0;
    map['oldStatus'] = oldStatus;
    map['isTake'] = isTake;
    map['canSelect'] = canSelect;
    map['num'] = number;
    map['attachIds'] = attachIds;
    return map;
  }
}
